<template>
    <div>
        <el-form 
        :model="replacementCard" 
        :rules="rules" ref="replacementCard" 
        v-loading="go"
        element-loading-text="正在跳转请稍等"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)" label-width="100px" class="demo-ruleForm">
            <el-row style=" padding-left:30px; padding-top:20px;">
                <el-col :span="24">
                    <div class="grid-content bg-purple-dark">
                        <br>
                        <div class="kuang"></div>
                        <span style=" padding-left:40px;" >{{title.title1}}</span>
                        <div class="xian"></div>
                        <div class="kuai">
                            <!-- 新增补卡start -->
                                    <el-form-item :label="title.name" prop="rulesName">
                                        <el-input v-model="replacementCard.rulesName" placeholder="请输入" style="width:400px;"></el-input>
                                    </el-form-item>
                                    <el-form-item :label="title.apply" prop="replacementApplyId">
                                        <el-select v-model="replacementCard.replacementApplyId" placeholder="请选择">
                                        <el-option
                                            v-for="item in options"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                        </el-option>
                                        </el-select>
                                    </el-form-item>
                                <!-- 选择负责人start -->
                                    <el-form-item :label="title.replacementHead" prop="replacementHead">
                                        <div class="avatar-uploader">
                                            <!-- 选中负责人后展示start -->
                                            <div v-if="selectedUser!=''" >
                                                <img  :src="selectedUser.icon" class="avatar" @click="headPage=true">
                                                <span style="position: relative;top: -17px;left: 20px;">{{ selectedUser.nickName }}
                                                    <span style="margin-left: 26px;cursor: pointer;" @click="deUser">X</span>
                                                </span>
                                            </div>
                                            <!-- 选中负责人后展示end -->
                                            <!-- 负责人未选中展示 -->
                                            <i v-else @click="headPage=true" class="el-icon-plus avatar-uploader-icon"></i>
                                        </div>
                                    </el-form-item>
                                    
                                    <el-dialog
                                        v-loading="loading"
                                        element-loading-text="拼命加载中"
                                        element-loading-spinner="el-icon-loading"
                                        element-loading-background="rgba(0, 0, 0, 0.8)"
                                        :visible.sync="headPage"
                                        width="50%"
                                        :show-close="false">
                                        <el-breadcrumb separator-class="el-icon-arrow-right">
                                            <!-- 步骤条start -->
                                            <el-breadcrumb-item  v-for="(value,index) in headNavs" :key="index+'a'" @click="upNav(index)">
                                                <el-button v-if="persons.id==undefined" size="mini" style="border: 0px;" @click="upNav(index)">{{ value }}</el-button>
                                            </el-breadcrumb-item>
                                            <!-- 步骤条end -->
                                            <br>
                                            <br>
                                            <br>
                                            <!-- 展示右边所选中的用户start -->
                                            <div style="float: right;margin-top: -70px;margin-right:30%;color: black;">
                                                已选择<span v-if="selectedUser!=''">1</span><span v-else>0</span>位联系人
                                                <br>
                                                <br>
                                                <br>
                                            </div>
                                            <!-- 展示选中的用户 -->
                                            <div  v-if="selectedUser!=''" class="architecture" style="height: 50px;float: right;    position: relative;top: 12px;right: 50px;">
                                                <img style="width: 30px;height: 30px;position: relative;top: 10px;" :src="selectedUser.icon"/>
                                                <span style="margin-left: 17%;color: black;color: black;font-size: 10px;display: block;margin-top: -20px;">
                                                    {{ selectedUser.nickName }}<br><br>
                                                    {{ selectedUser.tposition.deptName }}
                                                    <span style="float: right;margin-top: -10px;margin-right: 20px;cursor: pointer;" @click="deUser">X</span>
                                                </span>
                                                
                                            </div>
                                            <!-- 展示右边所选中的用户end -->
                                            <!-- 展示左边信息start -->
                                            <!-- 展示部门和人数start -->
                                            <div class="architecture" style="line-height: 39px;" v-for="(value,perIndex) in persons" :key="perIndex" @click="selectArchitecture(value.deptName)">
                                                <div class="log">
                                                    <img src="https://cdn7.axureshop.com/demo2023/2236859/images/%E8%AF%B7%E5%81%87/u2303.svg" />
                                                </div>
                                                <span style="margin-left: 5%;color: black;">
                                                    {{ value.deptName }}
                                                </span>
                                                &nbsp;&nbsp;&nbsp;({{ value.deptCount }})
                                                <span style="float: right;margin-right: 20px;">
                                                    ＞
                                                </span>
                                            </div>
                                            <!-- 展示部门和人数end -->
                                            <br>
                                            <!-- 展示当前所选部门的所有用户start -->
                                            <div v-if="users.length!=0">
                                                <div :class="[selectArchitectureCss != userIndex ?'architecture': '',selectArchitectureCss == userIndex ? 'architectureActe':'']" style="height: 50px;"  v-for="(myUser,userIndex) in users" :key="userIndex" @click="selectUser(userIndex)">
                                                    <el-radio style="width: 80%;height: 100%;" v-model="selectId" :label="userIndex">
                                                        &nbsp;&nbsp;&nbsp;
                                                        <img style="width: 30px;height: 30px;position: relative;top: 10px;" :src="myUser.icon"/>
                                                        <span style="margin-left: 5%;color: black;font-size: 10px;float: left;position: relative;top: 12px;left: 110px;">
                                                            {{ myUser.nickName }}<br><br>
                                                            {{ myUser.tposition.deptName }}
                                                        </span>
                                                        
                                                    </el-radio>
                                                    <span>
                                                            ＞
                                                    </span>
                                                </div>
                                            </div>
                                            <!-- 展示当前所选部门的所有用户start -->
                                            <!-- 展示左边信息end -->
                                            
                                        </el-breadcrumb>

                                        <!-- 竖直分割线 -->
                                        <div class="myHr"></div>
                                        
                                       
                                        <span slot="footer" class="dialog-footer">
                                            <el-button @click="headPage = false">取 消</el-button>
                                            <el-button type="primary" @click="headPage = false">确 定</el-button>
                                        </span>
                                    </el-dialog>
                                <!-- 选择负责人end -->
                                    <el-form-item :label="title.card" prop="card">
                                        <el-switch
                                            v-model="replacementCard.card" @change="reset">
                                        </el-switch>
                                    </el-form-item>

                                    <el-form-item :label="title.replacementCount" prop="replacementCount">
                                        每月最多可提交 
                                        <div style="width: 100px; display: inline;" @mouseover="replacementCountBoolen=true" @mouseleave="replacementCountBoolen=false">
                                            <el-input-number style="width: 100px;" v-model="replacementCard.replacementCount" :min="1" :disabled="!replacementCard.card" :controls="replacementCountBoolen" controls-position="right"></el-input-number>
                                        </div>
                                        次补卡
                                    </el-form-item>

                                    <el-form-item :label="title.time" prop="replacementCount">
                                        可申请过去 
                                        <div style="width: 100px; display: inline;" @mouseover="timeBoolen=true" @mouseleave="timeBoolen=false">
                                            <el-input-number style="width: 100px;" v-model="replacementCard.time" :min="1" :disabled="!replacementCard.card" :controls="timeBoolen" controls-position="right"></el-input-number>
                                        </div>
                                        天内的补卡
                                    </el-form-item>

                                    <el-form-item :label="title.replacementType" prop="replacementType">
                                        <el-checkbox-group v-model="replacementCard.replacementType" @change="demo()">
                                            <el-checkbox label="0">缺卡</el-checkbox>
                                            <el-checkbox label="1">迟到</el-checkbox>
                                            <el-checkbox label="2">早退</el-checkbox>
                                            <el-checkbox label="3">正常</el-checkbox>
                                        </el-checkbox-group>
                                    </el-form-item>
                                <el-form-item>
                                    <el-button @click="resetForm('replacementCard')" style="width: 127px;height: 36px;">取消</el-button>
                                    <el-button type="primary" @click="submitForm('replacementCard')" style="width: 127px;height: 36px;">提交</el-button>
                                </el-form-item>
                            <!-- 新增补卡end -->
                        </div>
                    </div>
                </el-col>
            </el-row>
        </el-form>
    </div>
  
</template>

<script>
import axios from 'axios'
export default {
    name:"replacementCardAdd",
    data(){
        return{
            // 登录成功跳转页面加载
            go:false,
            replacementCard:{
                rulesName:"",
                replacementHead:"",
                replacementType:[],
                card:false,
            },
            title:{
                title1:"规则信息",
                name:"规则名称",
                apply:"应用范围",
                replacementHead:"负责人",
                card:"允许补卡",
                replacementCount:"补卡次数",
                time:"补卡时限",
                replacementType:"补卡类型",
            },
            options: [{
                value: 0,
                label: '全公司考勤'
            }],
            rules: {
                rulesName: [
                    { required: true, message: '请输入规则名称', trigger: 'blur' }
                ],
                replacementHead:[
                    { required: true, message: '请输入选择负责人', trigger: 'blur' }
                ],
            },
            // 获取负责人start
            headPage:false,//开启选择负责人页面
            headNavs:["组织架构"],//步骤条
            persons:[],//部门和部门人数
            users:[],//所选部门的人员
            loading:false,//加载开启和关闭
            selectArchitectureCss:-1,//选中指定行后开启指定的css
            selectId:-1,//单选框
            selectedUser:"",//选中的用户
            //  获取负责人end

            // 鼠标是否悬浮在补卡次数input上面出现加减箭头
            replacementCountBoolen:false,
            // 同上只不过这个是补卡时限的
            timeBoolen:false,

        } 
    },
    created(){
        this.getReplacementApplyId();
        this.getDept();//获取部门
        let id = this.$route.params.id;
        if(id != undefined){
            this.getReplacementCard(id);
        }


    },
    methods: {
        // 获取应用范围
        getReplacementApplyId(){
            axios.post("/ReplacementCard/findTreeEntity")
            .then((res)=>{
                for (const iterator of res.data) {
                    this.options.push({
                        value:iterator.id,
                        label:iterator.label
                    });
                }
            })
        },
        // 提交事务
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.go = true;
                    this.replacementCard.replacementType = this.replacementCard.replacementType.toString();
                    // 判断用户id是否存在如果存在则修改反之
                    if(this.replacementCard.id==undefined){
                        // 获取用户名
                        axios.post("/user/findUserByToken")
                        .then((res)=>{
                            this.replacementCard.createdBy = res.data;
                            console.log(this.replacementCard)
                            axios.post("/ReplacementCard/addReplacementCard",this.replacementCard)
                            .then((res)=>{
                                if(res.data==1){
                                    this.$message({
                                        message:"新增成功",
                                        type:"success"
                                    });
                                    var myTime = setTimeout(()=>{
                                        res.data = -1;
                                        this.$router.go(-1);
                                    },3000);
                                }else{
                                    this.$message({
                                        message:"新增失败，请重试",
                                        type:"error"
                                    });
                                    this.go = false;
                                    setInterval(()=>{
                                        this.$router.go(0);
                                    },3000);
                                }
                                
                            })
                        })
                        
                    }else{
                        axios.post("/user/findUserByToken")
                        .then((res)=>{
                            this.replacementCard.updateBy = res.data;
                            axios.post("/ReplacementCard/upReplacementCard",this.replacementCard)
                            .then((res)=>{
                                this.go = true;
                                if(res.data==1){
                                    this.$message({
                                        message:"修改成功",
                                        type:"success"
                                    });
                                    var myTime = setTimeout(()=>{
                                        res.data = -1;
                                        this.$router.go(-1);
                                    },3000);
                                }else{
                                    this.$message({
                                        message:"新增失败，请重试",
                                        type:"error"
                                    });
                                    setInterval(()=>{
                                        this.$router.go(0);
                                    },3000);
                                }
                                
                            })
                        })
                        
                    }
                    
                    
                } else {
                    return false;

                }
            });
        },
        // 重置事务
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 负责人start
        // 更新步骤条
        upNav(index){
            this.headNavs = this.headNavs.slice(0,index+1);
            if("组织架构"==this.headNavs){
                this.loading = true;
                this.users = [];
                this.getDept();
            }
        },
        // 获取部门及部门人数
        getDept(){
            this.selectId=-1;
            this.deUser();
            axios.post("/ReplacementCard/findDept")
            .then((res)=>{
                this.persons = res.data.rows;
                this.loading = false;
            })
        },
        // 获取所选择部门的用户信息
        selectArchitecture(value){
            this.loading = true;
            this.persons = [];
            this.headNavs.push(value);
            var data = new FormData();
            data.append("name",value)
            axios.post("/ReplacementCard/findUserAndDept",data)
            .then((res)=>{
                this.users = res.data.rows;
                if(this.users!=[]){
                    this.loading = false;
                }
            })

        },
        // 是否允许补卡更改重置和他相关联的值
        reset(){
            if(!this.replacementCard.card){
                this.replacementCard.replacementCount = undefined;
                this.replacementCard.time = undefined;
            }
            
        },

        // 选中用户
        selectUser(index){
            this.selectArchitectureCss = index;
            this.selectedUser = this.users[index];
            this.replacementCard.replacementHead = this.selectedUser.id;
        },
        // 删除用户
        deUser(){
            this.selectId=-1;
            this.selectedUser="";
            this.selectArchitectureCss = -1;
            this.replacementCard.replacementHead = "";
        },
        // 负责人end
        
        //  获取指定补卡信息
        getReplacementCard(id){
            this.go = true;
            let data = new FormData();
            data.append("id",id);
            this.reset();
            axios.post("/ReplacementCard/findReplacementCard",data)
            .then((res)=>{
                this.replacementCard = res.data;
                this.replacementCard.replacementType = this.replacementCard.replacementType.toString().split(",");
                axios.post("/demo/findOneUser?id="+this.replacementCard.replacementHead)
                .then((res)=>{
                    this.selectedUser = res.data;
                    this.replacementCard.card = JSON.parse(this.replacementCard.card);
                    if(!this.replacementCard.card){
                        this.replacementCard.replacementCount = undefined;
                        this.replacementCard.time = undefined;
                        this.go = false;
                    }else{
                        this.go = false;
                    }
                }) 

            })
        },
        demo(){
        }

    }
}
</script>

<style scoped>
    body {
        margin: 0;
    }
    .kuai{
         width:1000px;
        height:400px;
        /* border: 1px red solid; */
        position: relative;
        top: 30px;
        left: 55px;
    }
    .kuai2{
         width:1000px;
        height:300px;
        /* border: 1px red solid; */
        position: relative;
        top: 30px;
        left: 55px;
    }
    .kuang{
        width: 3px;
        height: 20px;
        background-color: #409EFF;
        margin-left: 30px;
        position: relative;
        top: 23px;
    }
    .xian{
        width: 97.5%;
        height: 2px;
        background-color: #EBEEF5;
        margin-left: 40px;
        margin-top: 20px;
       
    }
    .el-row {
        margin-bottom: 20px;
    }
    .el-col {
        border-radius: 4px;
    }
    .bg-purple-dark {
        background: white;
        height: 1000px;
    }
    .bg-purple {
        background: #d3dce6;
    }
    .bg-purple-light {
        background: #e5e9f2;
    }
    .grid-content {
        
        border-radius: 4px;
        min-height: 36px;
    }
    .row-bg {
        padding: 10px 0;
        background-color: #f9fafc;
    }


    .avatar-uploader-icon[data-v-795f6812]{
        border: 1px solid rgba(198, 201, 208, 1);
        background-color: rgba(248, 248, 248, 1);
        border-radius: 50%;
    }
    .avatar-uploader-icon {
        font-size: 20px;
        color: #8c939d;
        width: 50px;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }

    /* 获取负责人start */
    .architecture{
        width: 271px;
        height: 39px;
        padding-left: 20px;
        border-radius: 5px;
    }
    .architecture:hover{
        background-color: #f8f8f8;
    }
    .architectureActe{
        width: 271px;
        height: 39px;
        line-height: 39px;
        padding-left: 20px;
        border-radius: 5px;
        background-color: rgba(231, 244, 255, 1);;
    }
    /* 竖着的水平线 */
    .myHr{
        border-left: 2px solid#989DAA;;
        height:100%;
        left: 48%;
        top: 0px;
        position: absolute;
    }

    .log{
        width: 27px;
        height: 27px;
        background-color: rgba(158, 208, 255, 1);
        float: left;
        position: relative;
        top: 15%;
        border-radius: 50%;
    }
    .log img{
        width: 13px;
        height: 13px;
        position: relative;
        bottom: 15%;
        left: 25%;
    }
    /* 获取负责人end */
</style>